package com.pig4cloud.pig.module.controller.examine;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.examine.ExamineApproveTemplateUser;
import com.pig4cloud.pig.module.api.entity.wx.WxUser;
import com.pig4cloud.pig.module.service.examine.IExamineApproveTemplateUserService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 审批流程（审核人）Controller
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Validated
@Api(value = "审批流程（审核人）控制器", tags = {"审批流程（审核人）管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/approve/template/user")
public class ExamineApproveTemplateUserController {

    private final IExamineApproveTemplateUserService examineApproveTemplateUserService;
    private final IWxUserService wxUserService;

    /**
     * 查询审批流程（审核人）列表
     */
    @ApiOperation("查询审批流程（审核人）列表")
    @PreAuthorize("@pms.hasPermission('module:user:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamineApproveTemplateUser> list(@Validated ExamineApproveTemplateUser examineApproveTemplateUser) {
        QueryWrapper<ExamineApproveTemplateUser> qw = new QueryWrapper<>();

        TableDataInfo<ExamineApproveTemplateUser> td = PageUtils.buildDataInfo(examineApproveTemplateUserService.page(PageUtils.buildPage(),qw));
        List<ExamineApproveTemplateUser> list = td.getData();
        return td;
    }

    /**
     * 导出审批流程（审核人）列表
     */
    @ApiOperation("导出审批流程（审核人）列表")
    @PreAuthorize("@pms.hasPermission('module:user:export')")
    @SysLog("审批流程（审核人）")
    @GetMapping("/export")
    public List export(@Validated ExamineApproveTemplateUser examineApproveTemplateUser) {
        List<ExamineApproveTemplateUser> list = examineApproveTemplateUserService.list();
        // ExcelUtil<ExamineApproveTemplateUser> util = new // ExcelUtil<ExamineApproveTemplateUser>(ExamineApproveTemplateUser.class);
        return  list;// util.exportExcel(list, "审批流程（审核人）");
    }

    /**
     * 获取审批流程（审核人）详细信息
     */
    @ApiOperation("获取审批流程（审核人）详细信息")
    @PreAuthorize("@pms.hasPermission('module:user:query')")
    @GetMapping("/{id}")
    public R<ExamineApproveTemplateUser> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ExamineApproveTemplateUser examineApproveTemplateUser = examineApproveTemplateUserService.getById(id);
        return R.ok(examineApproveTemplateUser);
    }

    /**
     * 新增审批流程（审核人）
     */
    @ApiOperation("新增审批流程（审核人）")
    @PreAuthorize("@pms.hasPermission('module:user:add')")
    @SysLog("审批流程（审核人）")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ExamineApproveTemplateUser examineApproveTemplateUser) {
        if (examineApproveTemplateUser.getUserId()!=null){
            WxUser wxUser=wxUserService.findById(examineApproveTemplateUser.getUserId());
            if (wxUser!=null){
                examineApproveTemplateUser.setUserName(wxUser.getNickName());
                examineApproveTemplateUser.setOpenId(wxUser.getOpenId());
                examineApproveTemplateUser.setUnionId(wxUser.getUnionId());
            }
        }
        if (examineApproveTemplateUser.getDutyName().equals("初审")){
            examineApproveTemplateUser.setDutyIndex(0);
        }
        if (examineApproveTemplateUser.getDutyName().equals("复审")){
            examineApproveTemplateUser.setDutyIndex(1);
        }
        if (examineApproveTemplateUser.getDutyName().equals("终审")){
            examineApproveTemplateUser.setDutyIndex(2);
        }
        if (examineApproveTemplateUser.getDutyName().equals("抄送")){
            examineApproveTemplateUser.setDutyIndex(3);
            examineApproveTemplateUser.setIsEnd(1);
            examineApproveTemplateUser.setIsCopy(1);
        }else {
            examineApproveTemplateUser.setIsEnd(0);
            examineApproveTemplateUser.setIsCopy(0);
        }
        return R.okByRow(examineApproveTemplateUserService.save(examineApproveTemplateUser) ? 1 : 0);
    }

    /**
     * 修改审批流程（审核人）
     */
    @ApiOperation("修改审批流程（审核人）")
    @PreAuthorize("@pms.hasPermission('module:user:edit')")
    @SysLog("审批流程（审核人）")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ExamineApproveTemplateUser examineApproveTemplateUser) {
        if (examineApproveTemplateUser.getUserId()!=null){
            WxUser wxUser=wxUserService.findById(examineApproveTemplateUser.getUserId());
            if (wxUser!=null){
                examineApproveTemplateUser.setUserName(wxUser.getNickName());
                examineApproveTemplateUser.setOpenId(wxUser.getOpenId());
                examineApproveTemplateUser.setUnionId(wxUser.getUnionId());
            }
        }
        if (examineApproveTemplateUser.getDutyName().equals("初审")){
            examineApproveTemplateUser.setDutyIndex(0);
        }
        if (examineApproveTemplateUser.getDutyName().equals("复审")){
            examineApproveTemplateUser.setDutyIndex(1);
        }
        if (examineApproveTemplateUser.getDutyName().equals("终审")){
            examineApproveTemplateUser.setDutyIndex(2);
        }
        if (examineApproveTemplateUser.getDutyName().equals("抄送")){
            examineApproveTemplateUser.setDutyIndex(3);
            examineApproveTemplateUser.setIsEnd(1);
            examineApproveTemplateUser.setIsCopy(1);
        }else {
            examineApproveTemplateUser.setIsEnd(0);
            examineApproveTemplateUser.setIsCopy(0);
        }
        return R.okByRow(examineApproveTemplateUserService.updateById(examineApproveTemplateUser) ? 1 : 0);
    }

    /**
     * 删除审批流程（审核人）
     */
    @ApiOperation("删除审批流程（审核人）")
    @PreAuthorize("@pms.hasPermission('module:user:remove')")
    @SysLog("审批流程（审核人）")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(examineApproveTemplateUserService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
